///////////////////////////////////////////////////////////////////////////////////
//基础通用接口封装-js接口模板引入
//开发人：虞胜强
//开发时间：2019/3/25
///////////////////////////////////////////////////////////////////////////////////
layui.config({
    base: '/js/module/'
}).extend({
    dtreeUtil: 'dtreeUtil',
    dialog: 'dialog',
    validUtil: 'validUtil',
    page: 'page',
    baseutil: 'baseutil',
    moment: 'moment',
    fileUtil: 'fileUtil',
    treetable: 'treetable',
    tiows: 'tiows',
    selectN: 'selectN',
    layarea: 'layarea'
});
var $, element;
layui.use(['form', 'jquery', 'table', 'laydate', 'validUtil', 'baseutil', 'laypage', 'dialog', 'element', 'page'], function () {
    var form = layui.form,
        validUtil = layui.validUtil,
        baseutil = layui.baseutil,
        dialog = layui.dialog,
        table = layui.table,
        page = layui.page;
    element = layui.element;
    $ = layui.jquery;

    /**
     * 工具栏-搜索框收起操作
     */
    $('#tool .hideMenu').click(function () {
        $(".cyt-retract-close").hide();
        $(".cyt-retract-open").show();
        $('.search-details .layui-form-item').hide();
        $(".search-details").hide();
    });
    /**
     * 工具栏-搜索框展开操作
     */
    $('#tool .showMenu').click(function () {
        $(".cyt-retract-close").show();
        $(".cyt-retract-open").hide();
        $(".search-details").show();
        $('.search-details .layui-form-item').show();
    });
    /**
     * 工具栏-增加弹窗
     */
    $('#tool .addBtn').click(function () {
        var url = $(this).attr('data-url');
        var width = $(this).attr('data-width');
        var height = $(this).attr('data-height');
        dialog.popUpPage("添加", url, width, height, function (layer, index) {
            var body = layer.getChildFrame('body', index);
            body.find("#save").bind("click", function () {
                parent.layer.closeAll();
                location.reload();
            });
            body.find("#cancel").bind("click", function () {
                parent.layer.closeAll();
            });

        });
        return false;
    });

    /**
     * 工具栏-tab跳转
     */
    $('#tool .jumpTabBtn').click(function () {
        var url = $(this).attr('data-url');
        var tabName = $(this).attr('data-tab-name');
        var id = $(this).attr('data-id');
        parent.tab(id, url, tabName);
    });

    /**
     * 工具栏-当前页面跳转
     */
    $('#tool .jumpBtn').click(function () {
        var url = $(this).attr('data-url');
        window.location.href = url;
        return false;
    });

    /**
     * 工具栏-删除
     */
    $('#tool .deleteBtn').click(function () {
        var url = $(this).attr('data-url');
        var ids = page.checkBoxCheckedIds();
        if (ids != null) {
            page.delete(url, {"id": ids, "_method": "DELETE"}, "您确定要删除选中项吗？", function (data) {
                table.reload("entityList", {
                    where: {}
                });
            });
        }
        return false;
    });

    /**
     * 工具栏-搜索
     */
    form.on('submit(seacherBtn)', function (data) {
        table.reload("entityList", {
            where: data.field
        });
        return false;
    });

    /**
     * 工具栏-导入
     */
    $('#tool .importBtn').click(function () {
        var download = $(this).attr('data-download');
        var url = $(this).attr('data-url');
        var name = $(this).attr('data-name');
        if (validUtil.isEmpty(name)) {
            name = "导入";
        }
        dialog.popUpPage(name, "/base/import?url=" + url + "&download=" + download, '450px', '320px', function (layer, index) {
            var body = layer.getChildFrame('body', index);
            body.find("#save").bind("click", function () {
                parent.layer.closeAll();
                location.reload();
            });
            body.find("#cancel").bind("click", function () {
                parent.layer.close(index);
            });
        });
    })
    //人员选择器
    $('.userSelect').click(function () {
        var resultId = $(this).attr('data-result-id');
        var resultName = $(this).attr('data-result-name');
        var type = $(this).attr('data-selector-type');
        var name = $(this).attr('data-name');
        if (validUtil.isEmpty(name)) {
            name = "人员选择器";
        }
        if (validUtil.isEmpty(type)) {
            type = "single";
        }
        selector(type, name, "/baseUser/dTreeList", resultId, resultName);
    })
    //菜单选择器
    $('.menuSelect').click(function () {
        var resultId = $(this).attr('data-result-id');
        var resultName = $(this).attr('data-result-name');
        var type = $(this).attr('data-selector-type');
        var name = $(this).attr('data-name');
        if (validUtil.isEmpty(name)) {
            name = "菜单选择器";
        }
        if (validUtil.isEmpty(type)) {
            type = "single";
        }
        selector(type, name, "/baseMenu/dTreeList", resultId, resultName);
    })
    //部门选择器
    $('.orgSelect').click(function () {
        var resultId = $(this).attr('data-result-id');
        var resultName = $(this).attr('data-result-name');
        var type = $(this).attr('data-selector-type');
        var name = $(this).attr('data-name');
        if (validUtil.isEmpty(name)) {
            name = "部门选择器";
        }
        if (validUtil.isEmpty(type)) {
            type = "single";
        }
        selector(type, name, "/sysOrganization/dTreeList", resultId, resultName);
    })

    function selector(type, name, url, resultId, resultName, w, h) {
        if (type == "single") {
            //单选
            url = "/base/selector/single?url=" + url;
        } else {
            //多选
            url = "/base/selector/mult?url=" + url;
            var params = [];
            params.push($("#" + resultId).val());//初始化选中对象id集合
            //初始化id值
            parent.cytSetSelectObjctParams(params);
        }
        if (validUtil.isEmpty(w)) {
            w = 380;
        }
        if (validUtil.isEmpty(h)) {
            h = 560;
        }
        if (validUtil.isEmpty(name)) {
            name = "选择器";
        }
        dialog.popUpPage(name, url, w + 'px', h + 'px', function (layer, index) {
            var body = layer.getChildFrame('body', index);
            body.find("#save").bind("click", function () {
                var id = body.find("#resultId").val();
                var name = body.find("#resultName").val();
                $("#" + resultId).val(id);
                $("#" + resultName).val(name);
                parent.layer.close(index);
            });
            body.find("#cancel").bind("click", function () {
                parent.layer.close(index);
            });
        });
        return false;
    }
});


/**
 * 传递参数
 */
var selectObjctParams = [];

function cytSelectObjctParams() {
    return selectObjctParams;
}

function cytSetSelectObjctParams(params) {
    selectObjctParams = params;
}

/**
 * 控制iframe窗口的刷新操作
 */
/*var iframeObjName;

//父级弹出页面
function page(title, url, obj, w, h, btnFn) {
    if (title == null || title == '') {
        title = false;
    }
    ;
    if (url == null || url == '') {
        url = "404.html";
    }
    ;
    if (w == null || w == '') {
        w = '700px';
    }
    ;
    if (h == null || h == '') {
        h = '350px';
    }
    ;
    iframeObjName = obj;
    //如果手机端，全屏显示
    if (window.innerWidth <= 768) {
        var index = layer.open({
            type: 2,
            title: title,
            area: [320, h],
            fixed: false, //不固定
            content: url,
            success: function (layero, index) {
                if (btnFn != null) {
                    btnFn(layer, index);
                }

            }
        });
        layer.full(index);
    } else {
        var index = layer.open({
            type: 2,
            title: title,
            area: [w, h],
            fixed: false, //不固定
            content: url,
            success: function (layero, index) {
                if (btnFn != null) {
                    btnFn(layer, index);
                }
            }
        });
    }
}*/

/**
 * 刷新子页,关闭弹窗
 */
/*function refresh() {
    //根据传递的name值，获取子iframe窗口，执行刷新
    if (window.frames[iframeObjName]) {
        window.frames[iframeObjName].location.reload();

    } else {
        window.location.reload();
    }

    layer.closeAll();
}*/


//父级tab页面
function tab(id, url, text) {
    var isActive = $('.main-layout-tab .layui-tab-title').find("li[lay-id=" + id + "]");
    if (isActive.length > 0) {
        //切换到选项卡
        element.tabChange('tab', id);
    } else {
        element.tabAdd('tab', {
            title: text,
            content: '<iframe src="' + url + '" name="iframe' + id + '" class="iframe" framborder="0" data-id="' + id + '" scrolling="auto" width="100%"  height="100%"></iframe>',
            id: id
        });
        element.tabChange('tab', id);

    }
}

function tabChange(id, url, text, oldId) {
    element.tabDelete('tab', oldId);
    var isActive = $('.main-layout-tab .layui-tab-title').find("li[lay-id=" + id + "]");
    if (isActive.length > 0) {
        element.tabChange('tab', id);
    } else {
        element.tabAdd('tab', {
            title: text,
            content: '<iframe src="' + url + '" name="iframe' + id + '" class="iframe" framborder="0" data-id="' + id + '" scrolling="auto" width="100%"  height="100%"></iframe>',
            id: id
        });
        element.tabChange('tab', id);
    }
}

function tabDelete(id) {
    element.tabDelete('tab', id);
}
//刷新指定页面
function tabRefresh(id) {
    var layId=$("#main-layout .main-layout-tab .layui-left-tabnav li.layui-this").attr("lay-id");
    if(layId!=null&&layId==id){
        $(".main-layout-body .layui-tab-content .layui-show").find("iframe")[0].contentWindow.location.reload();
    }else{
        element.tabChange('tab', id);
        $(".main-layout-body .layui-tab-content .layui-show").find("iframe")[0].contentWindow.location.reload();
    }
}
//获取当前页面id
function getTabId() {
    var layId=$("#main-layout .main-layout-tab .layui-left-tabnav li.layui-this").attr("lay-id");
    return layId;
}

function tabAdd(id, url, text) {
    var isActive = $('.main-layout-tab .layui-tab-title').find("li[lay-id=" + id + "]");
    if (isActive.length > 0) {
        element.tabDelete('tab', id);
    }
    element.tabAdd('tab', {
        title: text,
        content: '<iframe src="' + url + '" name="iframe' + id + '" class="iframe" framborder="0" data-id="' + id + '" scrolling="auto" width="100%"  height="100%"></iframe>',
        id: id
    });
    element.tabChange('tab', id);
}

//对Date的扩展，将 Date 转化为指定格式的String
//月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
//年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
//例子：
//(new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
//(new Date()).Format("yyyy-M-d h:m:s.S")      ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) {
    var o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

/**
 * 获取所有多选框选择的id
 * @author 虞胜强
 * @date 2018/2/8
 * */
function getAllId(obj) {
    var arr = [];
    obj.find(":checkbox[name='singleselect']").each(function (i, e) {
        var id = $(e).parents("tr").find("td:eq(0)").find('input').val();
        arr.push(id);
    });
    var ids = arr.join(",");
    return ids
}

function isBlank(value) {
    return (value === null || "" === value || "undefined" === value || undefined === value);
}

function isNotBlank(value) {
    return !isBlank(value);
}

